package com.door.erp.sevice.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.door.erp.constant.Constants;
import com.door.erp.dao.OutputOrderDao;
import com.door.erp.dao.OutputStatusDao;
import com.door.erp.dao.base.GenericDao;
import com.door.erp.model.OrderItem;
import com.door.erp.model.Orders;
import com.door.erp.model.OutputOrder;
import com.door.erp.model.OutputStatus;
import com.door.erp.sevice.GenerateNumberService;
import com.door.erp.sevice.OutputOrderService;
import com.door.erp.sevice.base.GenericServiceImpl;

@Service(value="outputOrderService")
public class OutputOrderServiceImpl extends
		GenericServiceImpl<OutputOrder, Long> implements OutputOrderService {

	@Resource(name="outputOrderDao", type=OutputOrderDao.class)
	private OutputOrderDao outputOrderDao;
	
	@Resource(name="outputStatusDao", type=OutputStatusDao.class)
	private OutputStatusDao outputStatusDao;
	
	@Resource(name="generateNumberService", type=GenerateNumberService.class)
	private GenerateNumberService generateNumberService;
	
	@Override
	public GenericDao<OutputOrder, Long> getDao() {
		return this.outputOrderDao;
	}
	
	@Override
	public void saveOutputOrders(Orders order) {
		if(order != null) {
			List<OrderItem> items = order.getItems();
			OutputOrder outputOrder = null;
			if(items == null) {
				return;
			}
			for(OrderItem item: items) {
				outputOrder = new OutputOrder();
				OutputStatus status = outputStatusDao.get(Constants.OUTPUT_DEFAULT_STATUS);
				outputOrder.setOutputStatus(status);
				outputOrder.setOutputOrderNo(generateNumberService.generateNum("S"));
				outputOrder.setProduct(item.getProduct());
				outputOrder.setDoorframe(item.getDoorframe());
				outputOrder.setDoorLeaf(item.getDoorLeaf());
				outputOrder.setLineShape(item.getLineShape());
				outputOrder.setOrders(item.getOrder());
				outputOrder.setColor(item.getColor());
				outputOrder.setCreateTime(new Date());
				this.outputOrderDao.add(outputOrder);
			}
		}
	}

}
